/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbf;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
//import com.mysql.jdbc.Connection;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rosi
 */
public class QueryDBF {

    String fileName = "E:/MS_PRODU.DBF";
    ArrayList<Object> kolom = new ArrayList<Object>();

    public void bacaFile() throws FileNotFoundException, IOException {

        try {
        
            FileInputStream inputStream = new FileInputStream(fileName);
            DBFReader reader = new DBFReader(inputStream);

            //baca nama kolom
            int jmlKolom = reader.getFieldCount();
            for (int i = 0; i < jmlKolom; i++) {

                DBFField field = reader.getField(i);
                System.out.print(field.getName() + "\t");
                kolom.add(field.getName());
            }
            System.out.println("\n");

            //baca isi kolom
            Object[] rowObjects;
            while ((rowObjects = reader.nextRecord()) != null) {
                for (int i = 0; i < rowObjects.length; i++) {
                    System.out.print(rowObjects[i] + "  ");
                }
                System.out.println();
            }

            inputStream.close();
        } catch (DBFException e) {
            System.out.println(e.getMessage());
        }
    }

    public void convert() {
//        String namaTabel = fileName;
        String namaKolom = "";
        String kolom1, kolom2, kolom3, kolom4, kolom5, kolom6, kolom7;
//        try{

//            for (Object nama : kolom) {
        for (int i = 0; i < kolom.size(); i++) {
            kolom1 = kolom.get(i).toString();
            System.out.println(kolom1);

        }
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String connString = "jdbc:odbc:Driver={Microsoft dBASE Driver (*.dbf)};DefaultDir=E:/MS_PRODU.DBF";//DeafultDir indicates the location of the db
//          String url = "jdbc:odbc:DRIVER={Microsoft dBase Driver (*.dbf)};DBQ=E:/FA_B39/GLMAST.DBF";
            Connection connection = DriverManager.getConnection(connString);
            String sql = "SELECT * FROM table_name where condition";// usual sql query
            Statement stmt = connection.createStatement();
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                System.out.println();
            }
            System.out.println();
        } catch (SQLException ex) {
            Logger.getLogger(QueryDBF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
        }
    }
}
